using System;
using Blazing.UI.Cms.Admin.Classes;
using Blazing.WebFacade;
using System.Web.UI.WebControls;
using CmsMenu = Blazing.BusinessObjects.Cms.Menu;
using CmsMenuItem = Blazing.BusinessObjects.Cms.MenuItem;

public partial class protected_ManageMenus : SiteBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            PanelAddMenu.Style.Add("display", "none");
            PanelAddMenuItem.Style.Add("display", "none");
        }
    }

    protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {

    }
    protected void LinkButtonInsertMenuItem_Click(object sender, EventArgs e)
    {
        DetailsViewMenuItem.Visible = true;
    }
    protected void DetailsViewMenuItem_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        PanelMenuItems.Visible = true;
    }
    protected void DetailsViewMenuItem_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values.Add("Menu", Blazing.BusinessObjects.Cms.Menu.Load((int)GridView1.SelectedValue));
        DropDownList parent = DetailsViewMenuItem.FindControl("DropDownList1") as DropDownList;
        if (parent != null && !string.IsNullOrEmpty(parent.SelectedValue))
        {
            e.Values.Add("Parent", Blazing.BusinessObjects.Cms.MenuItem.Load(int.Parse(parent.SelectedValue)));
        }
    }
    protected void GridViewMenuItems_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        e.NewValues.Add("Menu", Blazing.BusinessObjects.Cms.Menu.Load((int)GridView1.SelectedValue));
    }
    protected void GridViewMenuItems_DataBinding(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex > -1)
        {
            MenuEditor.Items.Clear();

            CmsMenu menu = CmsMenu.Load((int)GridView1.DataKeys[GridView1.SelectedIndex].Value);
            foreach (CmsMenuItem item in menu.ChildItems)
            {
                MenuItem mitem = new MenuItem(item.Label, item.ID.ToString(), null, "#none");
                MenuEditor.Items.Add(mitem);
                BuildMenuItems(item, mitem);
            }
        }
    }

    protected void BuildMenuItems(CmsMenuItem parent, MenuItem pitem)
    {
        if (parent.ChildItems != null)
        {
            foreach (CmsMenuItem item in parent.ChildItems)
            {
                MenuItem mitem = new MenuItem(item.Label, item.ID.ToString(), null, "#none");
                pitem.ChildItems.Add(mitem);
                BuildMenuItems(item, mitem);
            }
        }
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DropDownList menutype = DetailsView1.FindControl("DropDownListMenuTypes") as DropDownList;
        if (menutype != null && !string.IsNullOrEmpty(menutype.SelectedValue))
        {
            e.Values.Add("MenuType", Blazing.BusinessObjects.Cms.MenuType.Load(int.Parse(menutype.SelectedValue)));
        }
        e.Values.Add("Site", Blazing.BusinessObjects.Cms.Site.Load(SiteController.CurrentSiteId));
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        PanelMenuItems.Visible = false;
    }

    protected void odsMenuItems_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
    {

    }

    protected void odsMenuItems_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
    {

    }

    protected void odsMenuItems_Updated(object sender, ObjectDataSourceStatusEventArgs e)
    {
 
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        e.NewValues.Add("Site", Blazing.BusinessObjects.Cms.Site.Load(SiteController.CurrentSiteId));
    }
}
